package com.lyshop.jinxiang.activitys;

import static com.lyshop.jinxiang.constants.Constant.H5_PRODUCT_AGREEMENT;

import android.os.Build;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.UserInfoBean;
import com.lyshop.jinxiang.utils.DataUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class BuyAgreementActivity extends BaseActivity {

    private static final String TAG = "BuyAgreementActivity";
    private WebView mWebView;
    String product, code, count, model, price, order, fee;
    private String mLoadUrl = H5_PRODUCT_AGREEMENT + "?temp=" + System.currentTimeMillis();
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        product = (String) getIntent().getStringExtra("product");
        code = (String) getIntent().getStringExtra("code");
        count = (String) getIntent().getStringExtra("count");
        model = (String) getIntent().getStringExtra("model");
        price = (String) getIntent().getStringExtra("price");
        order = (String) getIntent().getStringExtra("order");
        fee = (String) getIntent().getStringExtra("fee");

        //替换内容
        userInfoBean = DataUtils.getUserInfo(mContext);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_buy_agreement;
    }

    @Override
    protected void findViews() {
        mWebView = $(R.id.wv_web);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebView.clearCache(true);
        mWebView.clearHistory();
        mWebView.setWebViewClient(new WebViewClient() {
            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                if (request.getUrl().toString().equals(mLoadUrl)) { // 检查是否是目标页面
                    try {
                        URL url = new URL(request.getUrl().toString());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        InputStream inputStream = connection.getInputStream();
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                        int i;
                        while ((i = inputStream.read()) != -1) {
                            byteArrayOutputStream.write(i);
                        }
                        String htmlContent = byteArrayOutputStream.toString();

                        // 替换${telephone}为实际的电话号码
                        String telephoneNumber = "1234567890";
                        String modifiedHtmlContent = htmlContent.replace("${order}", order);
                        // 多个值替换
                        Map<String, String> replacements = new HashMap<>();
                        replacements.put("${telephone}", userInfoBean.mobile);
                        replacements.put("${order}", order);
                        replacements.put("${product}", product);
                        replacements.put("${code}", code);
                        replacements.put("${count}", count);
                        replacements.put("${model}", model);
                        replacements.put("${price}", price);
                        replacements.put("${fee}", fee);

                        for (Map.Entry<String, String> entry : replacements.entrySet()) {
                            htmlContent = htmlContent.replace(entry.getKey(), entry.getValue());
                        }

                        return new WebResourceResponse("text/html", "UTF-8", new ByteArrayInputStream(htmlContent.getBytes()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return super.shouldInterceptRequest(view, request);
            }
        });
        mWebView.loadUrl(mLoadUrl);

    }

    @Override
    protected void setListeners() {

    }
}
